package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.auth.domain.SysRole;
import com.example.auth.domain.vo.SysRoleVo;
import com.example.auth.service.SysRoleService;
import com.example.config.exception.GlobalExcetion;
import com.example.util.Results;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;



    @GetMapping("list")
    public JSONObject list(){
        List<SysRole> sysRoles=sysRoleService.list();
        return Results.success(sysRoles);
    }


    @PostMapping("/save")
    public JSONObject save(@RequestBody @Validated SysRoleVo vo) throws GlobalExcetion {
        SysRole role=sysRoleService.getSysRoleByRoleNo(vo.getRoleNo());
        if(role!=null){
            throw new GlobalExcetion(1,"角色信息已存在");
        }
        SysRole role1=new SysRole();
        role1.setCreateTime(new Date());
        role1.setUpdateTime(new Date());
        role1.setIsDeleted(0);
        BeanUtils.copyProperties(vo,role1);
        sysRoleService.save(role1);
        return Results.success("角色创建成功");
    }
}
